package com.freight.user.serviceimpl;

import com.freight.core.common.exception.FreightException;
import com.freight.user.dao.CompanyDriverDao;
import com.freight.user.entity.CompanyDriver;
import com.freight.user.mapper.CompanyDriverMapper;
import com.freight.user.service.CompanyDriverService;
import com.freight.user.vo.CompanyRelationshipVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional
public class CompanyDriverServiceImpl  implements CompanyDriverService {

    @Autowired
    private CompanyDriverMapper companyDriverMapper;

    @Autowired
    private CompanyDriverDao companyDriverDao;

    @Override
    public void saveCompanyDriver(Long driverId, Long companyId) {
        List<CompanyDriver> cd = companyDriverMapper.getCompanyDriverVo(driverId,companyId);
        if(cd!=null && cd.size()>0){
            throw new FreightException("请勿重复分配该司机");
        }
        CompanyDriver companyDriver = new CompanyDriver();
        companyDriver.setDriverUserId(driverId);
        companyDriver.setCompanyUserId(companyId);
        companyDriverDao.save(companyDriver);
    }

    @Override
    public void deleteCompanyDriver(Long companyDriverId) {
        CompanyDriver companyDriver = new CompanyDriver();
        companyDriver.setId(companyDriverId);
        companyDriverDao.delete(companyDriver);

    }

    @Override
    public List<CompanyRelationshipVo> getCompanyDriverList(Long driverId) {
        return companyDriverMapper.getCompanyDriverList(driverId);
    }

    @Override
    public List<CompanyRelationshipVo> getCompanyRelationshipList(Long driverId) {


        return companyDriverMapper.getCompanyRelationshipList(driverId);

    }


}
